<%@page import="com.liferay.portal.kernel.util.StringPool"%>
<%@page import="com.liferay.portal.kernel.util.TextFormatter"%>
<%@page import="com.liferay.portal.model.Country"%>
<%@page import="java.util.List"%>
<%@page import="com.liferay.portal.service.CountryServiceUtil"%>
<%@include file="/html/init.jsp" %>

<%
	boolean isHost = GetterUtil.getBoolean(PortalUtil.getOriginalServletRequest(request).getParameter("host"), false);
	List<Country> countries = CountryServiceUtil.getCountries(true);
%>

<portlet:actionURL name="createHost" var="createHostURL"/>

<aui:form action="<%= createHostURL %>">

	<c:if test="<%= isHost %>">
		<aui:input name="host" type="hidden" value="<%= isHost %>" />
	</c:if>

	<aui:fieldset>
		<aui:row>
			<aui:column>
				<aui:input name="firstName" required="true" autoFocus="true"/>
			</aui:column>
			<aui:column>
				<aui:input name="lastName" />
			</aui:column>
		</aui:row>
		
		<aui:row>
			<aui:column>
				<aui:input name="emailAddress" required="true">
					<aui:validator name="email"/>
					<aui:validator name="custom" errorMessage="email-already-exists">
						function() {
							return emailNotExists();					
						}
					</aui:validator>					
				</aui:input>
			</aui:column>
			
			<%-- 
			<aui:column>
				<aui:select name="idd"/>
				<aui:input name="mobileNumber" cssClass="mobile-number" required="true" />
			</aui:column>
			--%>
			
			<aui:column>
				<div class="control-group">
					<label class="control-label" for="mobileNumber">
						<liferay-ui:message key="mobile-number"/> <span class="label-required">(<liferay-ui:message key="required"/>)</span>
					</label>		
						
					<aui:fieldset>
						<aui:column cssClass="idd-selection">
							<aui:select name="idd" label="">
								<%
									for (Country country: countries) {
										%><aui:option value="<%= country.getIdd() %>" label="<%= StringPool.PLUS + country.getIdd() %>" /><%
									}
								%>					
							</aui:select>
						</aui:column>
				
						<aui:column>
							<aui:input cssClass="mobile-number" name="mobileNumber" label="" required="true">
								<aui:validator name="digits"/>
								<aui:validator name="maxLength">10</aui:validator>
							    <aui:validator name="minLength">10</aui:validator>
							</aui:input>
						</aui:column>
					</aui:fieldset>			
				</div>			
			</aui:column>
		</aui:row>
		
		<aui:row>
			<aui:column>
				<aui:select name="gender" required="true" showEmptyOption="true">
					<aui:option value="1" label="male"/>
					<aui:option value="0" label="female"/>
				</aui:select>
			</aui:column>
			<aui:column>
				<aui:select name="nationality" required="true" showEmptyOption="true">
					<%
						for (Country country: countries) {
							%><aui:option value="<%= country.getCountryId() %>" label="<%= TextFormatter.format(country.getName(), TextFormatter.J) %>" /><%
						}
					%>
				</aui:select>
			</aui:column>
		</aui:row>		
				
		<aui:button type="submit"/>
	</aui:fieldset>
</aui:form>

<aui:script>
	function emailNotExists() {
		var frm = document.<portlet:namespace/>fm;
		var ele = frm.<portlet:namespace/>emailAddress;
	
		var ajaxURL = Liferay.PortletURL.createResourceURL();
		ajaxURL.setPortletId('register_WAR_mhstayportlet');
		ajaxURL.setParameter('<%= Constants.CMD %>', '<%= IConstants.CMD_CHECK_DUPLICATE %>');
		ajaxURL.setParameter('emailAddress', ele.value);
		ajaxURL.setWindowState('<%= LiferayWindowState.EXCLUSIVE.toString() %>');
		
		var notExists = true;
		AUI().io.request('<%= themeDisplay.getURLPortal() %>' + ajaxURL, {
			sync: true,
			on: {
				success: function() {
					notExists = (!(eval(this.get('responseData'))));
				}
			}
		});
		
		return notExists;
	}
</aui:script>